﻿using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Calc : UserControl
    {
        public Calc()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CompilerParameters vCompilerParameters = new CompilerParameters();

            vCompilerParameters.GenerateExecutable = false;

            vCompilerParameters.GenerateInMemory = true;

            string vSource = string.Empty;

            if (checkBox1.Checked)
            {
                vSource =
                @"public class Temp{
                    public int Test(){
                      return  " + textBox1.Text + @"; 
                    }
                }";


            }
            else
            {
                vSource =
                @"public class Temp{
                    public double Test(){
                      return " + textBox1.Text + @"; 
                    }
                }";

            }



            CompilerResults vCompilerResults =
                new CSharpCodeProvider().CompileAssemblyFromSource(vCompilerParameters, vSource);

            Assembly vAssembly = vCompilerResults.CompiledAssembly;

            object vTemp = vAssembly.CreateInstance("Temp");

            MethodInfo vTest = vTemp.GetType().GetMethod("Test");



            if (checkBox1.Checked)
            {
                int? num = (int)vTest.Invoke(vTemp, null);

                textBox2.Text = string.Format("{0:X}", num);

            }
            else
            {
                double? num = (double)vTest.Invoke(vTemp, null);

                textBox2.Text = string.Format("{0}", num);
            }

        }
    }
}
